<template>
    <div class='banner'>
        <div class="container">
            <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
                <el-form-item label="场景名称" prop="key_name">
                    <el-input v-model="queryParams.key_name" placeholder="请输入场景名称" clearable size="small"
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item>
                    <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
            <el-row :gutter="10" class="mb8">
                <el-col :span="22">
                    <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleBtn('add')"
                        v-hasPermi="['customer:manage:add']">
                        新增</el-button>
                    <!-- curRow={};title = '新增商品';handleShow = 'add'; -->
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table border v-loading="loading" :data="tableList" ref="tableList" v-tableHeight
                :height="tableList&&tableList.length > 0 ? '900px' : '120px'">
                <el-table-column label="排序" align="center" width="80">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.sort" @keyup.native="scope.row.sort=handleNum0(scope.row.sort)"
                            @blur="sortTab(scope.row)" clearable size="small" />
                    </template>
                </el-table-column>
                <el-table-column label="编号" align="center" prop="index" />
                <el-table-column label="主题名称" align="center" prop="title" />
                <el-table-column label="副标题" align="center" prop="sub_title" />
                <el-table-column label="图标" align="center" prop="cover">
                    <template slot-scope="scope">
                        <div v-if="scope.row.cover" class="img-list">
                            <el-image style="width: auto; height: 40px;" :src="scope.row.cover"
                                :preview-src-list="[scope.row.cover]">
                            </el-image>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="图片" align="center" prop="picture">
                    <template slot-scope="scope">
                        <div v-if="scope.row.picture" class="img-list">
                            <el-image style="width: auto; height: 40px;" :src="scope.row.picture"
                                :preview-src-list="[scope.row.picture]">
                            </el-image>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="状态" align="center" prop="status">
                    <template slot-scope="scope">
                        <span>
                            {{selectDictLabel(statusList, scope.row.status)}}
                        </span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" icon="el-icon-edit"
                            @click="handleBtn('edit', JSON.parse(JSON.stringify(scope.row)))"
                            v-hasPermi="['basic:history:edit']">
                            编辑
                        </el-button>
                        <el-button v-if="scope.row.status==0" size="mini" type="text" @click="handleAble(scope.row)"
                            v-hasPermi="['user:staff:stop']">停用
                        </el-button>
                        <el-button v-if="scope.row.status==1" size="mini" type="text" @click="handleAble(scope.row)"
                            v-hasPermi="['user:staff:start']">启用
                        </el-button>
                        <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                            v-hasPermi="['basic:history:del']">
                            删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
    
            <pagination v-show="pageCount>0" :pageCount="pageCount" :page.sync="queryParams.page"
                :limit.sync="queryParams.limit" @pagination="getList" />
            <add v-if="handleShow == 'add'||handleShow == 'edit'" :title="title" :handleShow="handleShow" :code="code"
                @close="code = ''; handleShow = ''" @load="getList" />
            <!-- <el-dialog :title="title" visible v-if="handleShow == 'add'||handleShow == 'edit'" @close="close()" width="600px" append-to-body
                    :close-on-click-modal="false">
                    <el-form ref="form" :model="form" :rules="rules" label-width="120px" v-loading="formLoading">
                        <el-row> 
                            <el-col :span="24">
                                <el-form-item prop="title" label="主题名称">
                                    <el-input style="width: 215px;" v-model="form.title" placeholder="请输入主题名称" clearable size="small" />
                                </el-form-item>
                            </el-col>
                            <el-col :span="24" v-if="form.type||form.type=='0'">
                                <el-form-item label="主题图片" prop="url">
                                    <el-upload class="avatar-uploader" :action="$uploadUrl" :show-file-list="false"
                                        :before-upload="beforeUpload" :on-success="handleAvatarSuccess">
                                        <img v-if="form.url" :src="form.url" class="avatar">
                                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                        <div slot="tip" class="el-upload__tip text-error" v-if="form.type=='0'">请上传图片尺寸为694*249的图片</div>
                                        <div slot="tip" class="el-upload__tip text-error">单文件大小请在1M以内,仅支持JPG、JPEG、PNG格式</div>
                                    </el-upload>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <span slot="footer" class="dialog-footer">
                        <el-button @click="close()">取 消</el-button>
                        <el-button type="primary" @click="submit" :disabled="formLoading">确 定</el-button>
                    </span>
                </el-dialog> -->
        </div>
    
    </div>
</template>
<script>
import throttle from 'lodash.throttle'
import { getBannerLists, setBannerInfo, getBannerInfo, setBannerStatus, delBanner } from "@/api/basic/banner.js"
import { getSceneLists, getSceneInfo, setSceneStatus, setSceneDel, setSceneSort } from "@/api/goods/scenarized.js"
import { departStatus } from "@/api/dictionary"
import add from './handle/add2';
export default {
    name: '',
    components: {
        add
    },
    data() {
        return {
            showSearch: true,
            loading: false,
            pageCount: 0,
            total: 0,
            queryParams: {
                page: 1,
                limit: 20,
                key_name: '',
            },
            tableList: [],
            title: "",
            handleShow: '',
            formLoading: false,
            form: {
                type: '',
                title: '',
                url: '',
                jump_type: '',
                jump_target: '',
            },
            rules: {
                type: [{ required: true, message: '请选择图片类型', trigger: 'blur' }],
                title: [{ required: true, message: '请输入图片标题', trigger: 'change' }],
                url: [{ required: true, message: '请选择上传图片', trigger: 'change' }],
                jump_type: [{ required: true, message: '请选择跳转类型', trigger: 'blur' }],
                jump_target: [{ required: true, message: '请输入跳转目标', trigger: 'change' }],
            },
            statusList: departStatus,
            curRow: {},
            code: ''
        }
    },
    created() {
        this.getList()
    },
    methods: {
        getList() {
            this.loading = true;
            getSceneLists(this.queryParams).then(res => {
                console.log(res.data, "res.data")
                this.tableList = res.data.lists;
                this.pageCount = res.data.pageCount;
                this.loading = false;
            });
        },
        handleBtn(type, row) {

            this.title = type == "add" ? "新增" : "编辑"

            if (row) {
                this.code = row.code

            }
            this.handleShow = type;
        },

        handleAble(row) {
            let label = row.status == 0 ? '停用' : '启用';
            this.$confirm(`是否将“${row.title}”${label}?`, "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return setSceneStatus({ code: row.code });
            }).then(() => {
                this.getList();
                this.msgSuccess(`${label}成功`);
            })
        },
        handleDelete(row) {
            this.$confirm('是否确认删除' + '"' + row.title + '"' + '?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return setSceneDel({ codes: row.code });
            }).then(() => {
                this.getList();
                this.msgSuccess("删除成功");
            })
        },

        sortTab(row) {
            console.log(row, 123)
            setSceneSort({
                sort: row.sort,
                code: row.code
            }).then(res => {
                if (res.code == 1) {
                    this.msgSuccess(res.info);
                    this.getList()
                }
            })
        },
        close() {
            this.form = {}
            this.handleShow = ""
        },
        handleQuery() {
            this.queryParams.page = 1;
            this.getList();
        },
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        changePic(e) {
            this.$set(this.form, "url", null)
        },
        beforeUpload(file, fileList) {
            return new Promise((resolve, reject) => {
                if (file.size / (1024 * 1024) > 1) {  // 限制文件大小
                    this.$message.warning(`当前限制文件大小不能大于1M`)
                    return reject(false)
                }
                if (this.form.type == '0') {
                    this.limitFileWH(694, 249, file).then((res) => {
                        file.isFlag = res
                        if (file.isFlag) {
                            return resolve(true)
                        } else {
                            return reject(false)
                        }
                    })
                } else {
                    resolve(true)
                }
            })
        },
        handleAvatarSuccess(res, file) {
            this.$set(this.form, 'url', res.data[0].fileurl)
        },
        submit: throttle(function () {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.formLoading = true;
                    setBannerInfo(this.form).then(res => {
                        this.msgSuccess("操作成功！");
                        this.close()
                        this.formLoading = false;
                        this.getList();
                    }).catch(() => {
                        this.formLoading = false;
                    })
                }
            })
        }, 1000, { trailing: false }),

    },
}

</script>
<style scoped lang='scss'>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    width: 100px;
    height: 100px;
}

.avatar-uploader .el-upload:hover {
    border-color: #409eff;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    /* text-align: center;
    display: block !important; */
}

.avatar {
    width: 100px;
    height: 100px;
    display: block;
}
</style>